package com.eleaf.adbb.template.android;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import android.view.View;

import com.eleaf.adbb.model.LayoutType;
import com.eleaf.adbb.template.ClassTemplate;
import com.eleaf.adbb.template.MethodTemplate;
import com.eleaf.adbb.template.VariableTemplate;
import com.eleaf.adbb.util.BaseUtil;

public class CodeTemplate extends ClassTemplate {

	private String lastPackage, layoutName, hostPackage;

	private List<PropertyTemplate> propertys;
	MethodTemplate onCreate;
	MethodTemplate onCreateView;
	MethodTemplate onViewCreated;

	public CodeTemplate( LayoutType type, String layoutName, String hostPackage, boolean useRoboGuice ) {
		super ( type, null );
		propertys = new ArrayList<PropertyTemplate> ();
		this.layoutName = layoutName;
		this.hostPackage = hostPackage;
		setUseRoboGuice ( useRoboGuice );

		List<String> parameters = new ArrayList<String> ();
		parameters.add ( "Bundle" );
		List<String> packages = new ArrayList<String> ();
		packages.add ( "android.os.Bundle" );
		packages.add ( BaseUtil.appendString ( hostPackage, ".R" ) );

		onCreate = new MethodTemplate ( type == LayoutType.ACTIVITY ? Modifier.PROTECTED : Modifier.PUBLIC, "void", "onCreate", parameters, packages );
		onCreate.insertCode ( BaseUtil.appendString ( "super.onCreate ( ", onCreate.getParameterName ( 0 ), " );" ) );
		if ( isUseRoboGuice () ) {
			addAnnotation ( BaseUtil.appendString ( "@ContentView(R.layout.", layoutName, ")" ) );
			addImport ( "roboguice.inject.ContentView" );
		} else if ( type == LayoutType.ACTIVITY ) {

			onCreate.insertCode ( BaseUtil.appendString ( "setContentView(R.layout.", layoutName, ");" ) );
		}
		addMethod ( onCreate );

		if ( type == LayoutType.FRAGMENT ) {

			List<String> onCreateViewPackages = new ArrayList<String> ();
			onCreateViewPackages.add ( "android.view.LayoutInflater" );
			onCreateViewPackages.add ( "android.view.View" );
			onCreateViewPackages.add ( "android.view.ViewGroup" );

			List<String> onCreateViewParameters = new ArrayList<String> ();

			onCreateViewParameters.add ( "LayoutInflater" );
			onCreateViewParameters.add ( "ViewGroup" );
			onCreateViewParameters.add ( "Bundle" );

			VariableTemplate mRootView = new VariableTemplate ( Modifier.PRIVATE, "mRootView", View.class );
			addVariable ( mRootView );
			onCreateView = new MethodTemplate ( Modifier.PUBLIC, "View", "onCreateView", onCreateViewParameters, onCreateViewPackages );
			onCreateView.insertCode ( BaseUtil.appendString ( "mRootView=", onCreateView.getParameterName ( 0 ), ".inflate ( R.layout.", getLayoutName (), ",",
					onCreateView.getParameterName ( 1 ), ", false );" ) );
			onCreateView.setRetunCode ( "mRootView" );
			addMethod ( onCreateView );

			List<String> onViewCreatedViewParameters = new ArrayList<String> ();

			onViewCreatedViewParameters.add ( "View" );
			onViewCreatedViewParameters.add ( "Bundle" );

			onViewCreated = new MethodTemplate ( Modifier.PUBLIC, "void", "onViewCreated", onViewCreatedViewParameters, new ArrayList<String> () );
			onViewCreated.insertCode ( BaseUtil.appendString ( "super.onViewCreated ( ", onViewCreated.getParameterName ( 0 ), ",", onViewCreated.getParameterName ( 1 ), " );" ) );
			addMethod ( onViewCreated );

		}

	}

	public String getLastPackage() {
		return lastPackage;
	}

	public void setLastPackage( String lastPackage ) {
		this.lastPackage = lastPackage;
	}

	public String getLayoutName() {
		return layoutName;
	}

	public void setLayoutName( String layoutName ) {
		this.layoutName = layoutName;
	}

	public String getHostPackage() {
		return hostPackage;
	}

	public void setHostPackage( String hostPackage ) {
		this.hostPackage = hostPackage;
	}

	@Override
	public String getPackagePath() {
		return BaseUtil.appendString ( hostPackage, ".", lastPackage );
	}

	public List<PropertyTemplate> getPropertys() {
		return propertys;
	}

	public void addProperty( List<PropertyTemplate> propertys ) {
		if ( propertys != null ) {
			for ( PropertyTemplate pt : propertys ) {
				addProperty ( pt );
			}
		}
	}

	public void addProperty( PropertyTemplate property ) {
		if ( property != null ) {
			this.propertys.add ( property );
			if ( isUseRoboGuice () ) {
				String annotation;
				if ( property.isFragment () ) {
					annotation = BaseUtil.appendString ( "@InjectFragment(R.id.", property.getPropertyId (), ")" );
					addImport ( "roboguice.inject.InjectFragment" );
				} else {
					annotation = BaseUtil.appendString ( "@InjectView(R.id.", property.getPropertyId (), ")" );
					addImport ( "roboguice.inject.InjectView" );
				}
				property.addAnnotation ( annotation );
			} else {
				if ( property.isFragment () ) {
					// TODO FRAGMENT
					String line = null;
					if ( getLayoutType () == LayoutType.FRAGMENT ) {
						line = BaseUtil.appendString ( property.getVariableType (), " ", property.getVariableName (), "= (", property.getVariableType (),
								")getFragmentManager ().findFragmentById ( R.id.", property.getPropertyId (), " );" );
						if ( onViewCreated != null ) {
							onViewCreated.insertCode ( line );
						}
					} else {

					}
				} else {
					String line = null;
					if ( getLayoutType () == LayoutType.ACTIVITY ) {
						line = BaseUtil.appendString ( property.getVariableName (), "=(", property.getVariableType (), ")findViewById(R.id.", property.getPropertyId (), ");" );
						if ( onCreate != null ) {
							onCreate.insertCode ( line );
						}
					} else {
						line = BaseUtil.appendString ( property.getVariableName (), "=(", property.getVariableType (), ")mRootView.findViewById(R.id.", property.getPropertyId (),
								");" );
						if ( onViewCreated != null ) {
							onViewCreated.insertCode ( line );
						}
					}

				}
			}
			addVariable ( property );
			addImport ( property.getPackagePath () );
		}
	}
}
